package cn.xuchunh.campusbbs.adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.Locale;

import cn.xuchunh.campusbbs.AppContext;
import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.bean.Indicator;
import cn.xuchunh.campusbbs.bean.User;
import cn.xuchunh.campusbbs.viewholder.PersonHeaderViewHolder;
import cn.xuchunh.campusbbs.viewholder.SimpleIconViewHolder;

/**
 * Created by XuChunH on 2015/10/18.
 */
public class PersonAdapter extends BaseRecyclerViewAdapter<ViewHolder, Indicator> {

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_CONTENT = 1;
    private int paddingVertical;

    public PersonAdapter(Context context) {
        super(context);
        paddingVertical = context.getResources().getDimensionPixelSize(R.dimen.content_margin)/2;
    }

    @Override
    public ViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new PersonHeaderViewHolder(inflater.inflate(R.layout.item_person_header, parent, false));
        } else {
            return new SimpleIconViewHolder(inflater.inflate(R.layout.item_simple_icon, parent, false));
        }
    }

    @Override
    protected void onBindContentViewHolder(ViewHolder holder, int position) {
        if (position == 0) {
            PersonHeaderViewHolder personHeaderViewHolder = ((PersonHeaderViewHolder) holder);
            User user = AppContext.getInstance().getUser();
            personHeaderViewHolder.avatar.setImageURI(Uri.parse(user.getAvatar()));
            personHeaderViewHolder.userName.setText(user.getUserName());
            personHeaderViewHolder.credits.setText(String.format(Locale.CHINA, context.getString(R.string.person_credits_format), user.getCredits(), user.getExtCredits()));
            personHeaderViewHolder.userTitle.setText(user.getUserTitle());
        } else {
            SimpleIconViewHolder simpleIconViewHolder = (SimpleIconViewHolder) holder;
            Indicator indicator = dataList.get(position - 1);
            if (indicator.getType() == Indicator.TYPE_INDICATOR) {
                simpleIconViewHolder.icon.setImageURI(indicator.getUri());
                simpleIconViewHolder.title.setText(indicator.getTitle());
            } else {
                simpleIconViewHolder.container.setVisibility(View.GONE);
                FrameLayout frameLayout = ((FrameLayout) simpleIconViewHolder.itemView);
                frameLayout.setPadding(0, paddingVertical, 0, paddingVertical);
                frameLayout.setBackgroundColor(context.getResources().getColor(R.color.default_divider_dark));
            }

        }
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof SimpleIconViewHolder) {
            SimpleIconViewHolder simpleIconViewHolder = ((SimpleIconViewHolder) holder);
            simpleIconViewHolder.container.setVisibility(View.VISIBLE);
            FrameLayout frameLayout = ((FrameLayout) simpleIconViewHolder.itemView);
            frameLayout.setPadding(0, 0, 0, 0);
            frameLayout.setBackgroundColor(Color.parseColor("#00000000"));
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size() == 0 ? 0 : dataList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        } else {
            return TYPE_CONTENT;
        }
    }
}
